/*
Spotter Exercise Manager
Copyright 2011 Evan Nelson.

This file is part of Spotter Exercise Manager.

Spotter Exercise Manager is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Spotter Exercise Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Spotter Exercise Manager.  If not, see <http://www.gnu.org/licenses/>.

The author can be contacted by emailing ean5533@gmail.com
*/

#include <QSqlQuery>
#include <QDate>
#include <QVariant>

#include "plotter.h"
#include "database.h"
#include "settings.h"

#include <qwt_plot_curve.h>
#include <qwt_scale_draw.h>
#include <qwt_legend.h>
#include <qwt_symbol.h>

//A quick class to display labels as dates instead of numbers
class DateScaleDraw: public QwtScaleDraw
{
public:
    DateScaleDraw()
    {
    }
    virtual QwtText label(double v) const
    {
        return QDate::fromJulianDay(v).toString(Settings::DATE_FORMAT);
    }
};

Plotter::Plotter()
{
}

void Plotter::buildEmptyPlot(QwtPlot *plot) {
    plot->detachItems();
    plot->setTitle("No graph selected");
}

void Plotter::buildWtOverTimePlot(QwtPlot *plot, bool legend, QStringList exercises, QDate *minDate, QDate *maxDate) {
    plot->detachItems();
    plot->setTitle("Lifts Over Time (1-Rep Max)");

    int curHue = 0; //used for generating new plot colors

    QwtSymbol sym;
    sym.setStyle(QwtSymbol::Ellipse);
    sym.setSize(5);

    for(int i = 0; i < exercises.size(); i++) {

        QString item = exercises.at(i);
        int indexWtType = item.lastIndexOf("(");
        QString exercise = item.mid(0, indexWtType-1);
        QString wtType = item.mid(indexWtType+1, item.length() - indexWtType - 2);

        QString sQuery = "SELECT dateRecorded, max(oneRepMax) FROM "
                        "   (SELECT oneRepMax, dateRecorded FROM Weightlifting w"
                        "   JOIN WeightLog l ON w.logID = l.LID"
                        "   WHERE status = 'Completed'";

        if(minDate != 0) {
            sQuery +=   "   AND dateRecorded >= '" + minDate->toString(Settings::DATE_FORMAT) + "' ";
        }
        if(maxDate != 0) {
            sQuery +=   "   AND dateRecorded <= '" + maxDate->toString(Settings::DATE_FORMAT) + "' ";
        }

        sQuery +=       "   AND exercise = '" + exercise + "'"
                        "   AND weightType = '" + wtType + "') "
                        " GROUP BY dateRecorded";

        QSqlQuery *query = Database::prepareQuery(sQuery);
        query->exec();

        QVector<double> dates;
        QVector<double> weights;
        while(query->next()) {
            double date = QDate::fromString(query->value(0).toString(), Settings::DATE_FORMAT).toJulianDay();
            dates.append(date);
            weights.append(query->value(1).toDouble());
        }


        QPen pColor(QColor::fromHsv(curHue, 255, 190));
        sym.setPen(pColor);

        QwtPlotCurve *curve = new QwtPlotCurve(exercises.at(i));
        curve->setData(dates, weights);
        curve->setRenderHint(QwtPlotItem::RenderAntialiased);
        curve->setPen(pColor);
        curve->setSymbol(sym);
        curve->attach(plot);

        curHue = (curHue + 50) % 359;
    }

    plot->setAxisTitle(QwtPlot::yLeft, "Weight");
    plot->setAxisTitle(QwtPlot::xBottom, "Date");
    plot->setAxisScaleDraw(QwtPlot::xBottom, new DateScaleDraw()); //reset x axis to use date labels (instead of numbers)
    plot->setAxisLabelRotation(QwtPlot::xBottom, -50.0);
    plot->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);

    if(legend) {
        plot->insertLegend(new QwtLegend(), QwtPlot::RightLegend);
    }

    plot->replot();
}
